package admin.subject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.BaseModel;
import com.common.CommonUtil;
import com.common.ResultData;
import com.filter.BaseController;
import com.intercepter.AuthPassport;

import beans.subject.subsetquestion;
import beans.subject.subsetquestionModel;
import beans.subject.subsetquestionsubject;
import beans.sys.SYSCodeBean;
import beans.sys.UserBean;
import service.subject.SubSetQuestionImpl;
import service.subject.SubSetQuestionSubjectImpl;
import service.sys.SYSCodeServiceImpl;
@Controller
@RequestMapping("/setquestion")
public class SetquestionController extends BaseController{
	@Resource
	private SubSetQuestionImpl<subsetquestion> _SubSetQuestionImpl;
	
	@Resource
	private SubSetQuestionSubjectImpl<subsetquestionsubject> _SubSetQuestionSubjectImpl;
	
	@Resource
	private SYSCodeServiceImpl<SYSCodeBean> _SYSCodeServiceImpl;
	
	@AuthPassport(actionName="index",controllerName="special")
	@RequestMapping("/index")
	public String Index() {
		List<Map> list = _SYSCodeServiceImpl.getListByCode("CarType");
		getReq().setAttribute("CarTypeList", list);
		
		return "admin/setquestion/index";
	}
	
	@RequestMapping("/GetData")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="special")
	public void DataGrid(subsetquestionModel model){
		ResultData<List<subsetquestion>> rd = new ResultData<>();
		try{
			UserBean u = (UserBean)getReq().getSession().getAttribute("user");

			rd.setPage(model.getPage());

			List<subsetquestion> m = _SubSetQuestionImpl.queryByList(model);
			
			List<String> ids = new ArrayList<>();
			for(subsetquestion setquestion : m) {
				ids.add(setquestion.getId());
			}
			if(ids.size()>0) {
				List<Map> mList = _SubSetQuestionSubjectImpl.queryCountBysqid(ids);
				for(subsetquestion setquestion : m) {
					for(Map tem : mList) {
						if(setquestion.getId().equals(tem.get("sqid").toString())) {
							setquestion.setSubcount(Integer.valueOf(tem.get("count").toString()));
							break;
						}
					}
				}
			}
			
			rd.setRows(m);
			rd.setRecords(model.getPager().getRowCount());
			rd.setTotal(model.getPager().getPageCount());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		responseJson(rd);
	}

	@AuthPassport(actionName="index",controllerName="special")
	@RequestMapping("/detail")
	public String Detail(String id) {
		subsetquestion m = null;
		if(!CommonUtil.isNullObject(id)){
			try {
				m = _SubSetQuestionImpl.queryById(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		List<Map> list = _SYSCodeServiceImpl.getListByCode("CarType");

		getReq().setAttribute("CarTypeList", list);
		getReq().setAttribute("chapter", m);
		return "admin/setquestion/detail";
	}

	@AuthPassport(actionName="index",controllerName="special")
	@RequestMapping("/save")
	@ResponseBody
	public void Save(subsetquestion m) {
		ResultData<String> rd = new ResultData<>();
		try {
			UserBean u = (UserBean)getReq().getSession().getAttribute("user");
			if(CommonUtil.isNullObject(m.getId())) {
				m.setId(UUID.randomUUID().toString());
				m.setSubcount(0);
				_SubSetQuestionImpl.add(m);
			}
			else {
				_SubSetQuestionImpl.update(m);
			}
			rd.setType(1);
			rd.setMessage("保存成功!");
		} catch (Exception e) {
			rd.setMessage("系统错误,请联系管理员!");
			e.printStackTrace();
		}
		responseJson(rd);
	}

	@AuthPassport(actionName="index",controllerName="special")
	@RequestMapping("/delete")
	@ResponseBody
	public void Delete() {
		ResultData<String> rd = new ResultData<>();
		String[] ids = getReq().getParameterValues("ids");
		try{
			_SubSetQuestionImpl.deletes(ids);
			rd.setType(1);
			rd.setMessage("删除成功");
		}
		catch(Exception e){
			e.printStackTrace();
			rd.setMessage("删除失败");
		}
		responseJson(rd);
	}
}
